#!/usr/bin/perl
use Benchmark qw(:all);
use FindBin qw($Bin);
use Data::Dumper;

BEGIN {

	# Set our common library path.
	unshift @INC, $Bin . '/../lib';
}

# Get the run start time.
my $t0 = Benchmark->new;

# Cpan common library
use strict;
use warnings;
use utf8;
use Encode;
use HTML::LinkExtor;
use HTML::TreeBuilder::XPath;
use LWP::Simple;
use Digest::MD5 qw(md5_hex);

# Our common library
use Module::Module;
use File::Data;
use Date::Time;
use Feed::XML;
use Config::Config;
use Log::Write;
use Help::Message;
use File::TreeConstructor;

# Initialize run environment
# Global pram.
our $basepath      = "$Bin/..";
our $programname   = '';
our $programstatus = 0;

# Default execute parameters.
our %EXEC = (
	'n' => 0,    # Sitename.
	'l' => 0,    # Print site list.
	'h' => 0,    # Print help information.
	'v' => 0,    # Print version.
	'i' => 1,    # Execute initialize function.
	'f' => 1,    # Execute filter function.
	'p' => 1,    # Execute parser function.
	'd' => 1,    # Execute download images function.
	'r' => 1,    # Execute remove stale images function.
	'c' => 1,    # Execute check feed function.
	'e' => 1,    # Execute send report email function.
	'b' => 1
);               # Execute build tar function.

foreach my $param (@ARGV) {
	if ( $param =~ /^-([^-])([^-]*?)$/ ) {
		$EXEC{$1} = $2;
		$EXEC{$1} = 1 if ( $1 eq 'h' );
		$EXEC{$1} = 1 if ( $1 eq 'l' );
		$EXEC{$1} = 1 if ( $1 eq 'v' );
	}
	else {
		$programname = $param;
	}
}
if ( $EXEC{l} ) {
	print Help::Message::getModuleList();
	$EXEC{e} = 0;
	exit;
}
elsif ( $EXEC{v} ) {
	print Help::Message::getVersion();
	$EXEC{e} = 0;
	exit;
}
elsif ( $EXEC{h} || scalar(@ARGV) == 0 || $programname eq '' ) {
	print Help::Message::getHelp();
	$EXEC{e} = 0;
	exit;
}
elsif ( !Help::Message::checkModuleExist($programname) ) {
	print "site is not exist!\n";
	$EXEC{e} = 0;
	exit;
}

# Load config file according to program name.
my $conf = new Config::Config;
our $memory   = {};
our $config   = $conf->getConfig( $Bin . '/../conf', $programname );
our $loginfo  = $conf->getIni( $config->{'log_info_file'} );
our $filedata = new File::Data();

# Set program name to config hash.
$config->{'programname'} = $programname;
$config->{'lockfile'}    = $config->{'base_path'} . "/data/$programname/lock";

# Set debug mode.
$config->{'mode'} = 'debug' if ( -e $config->{'base_path'} . '/debug' );

# Default package list.
my @requirefile =
  ( $config->{'base_path'} . '/module/' . $programname . '/parser.pm' );

# Require default package.
foreach my $filedir (@requirefile) {
	if ( -e $filedir ) {
		require($filedir);
	}
}

# Create a new object of module class.
my $module = new { 'module::' . $programname . '::parser' };
$module->{'config'} = $config;

# Run start
# A000000 : Crawler process starts runing,and process id is recorded.
Log::Write::setLog( 'A000000', { 'PID' => $$ } );

# Start up the module.
#$module->boot();

eval {

    print "##################################################################\n";
	# Initialize crawler.
#	$module->init() if ( $EXEC{i} );
#	my $str = "Englist=Chinese";
#	if($str =~ /([^=]*)=([^=]*)/is){
#		my $temp1	= $1;
#		my $temp2	= $2;
#		print $temp1;
#		print $temp2;
#		print "111";
#	}
	# Parser detail page ,get product infomation.
	my $url     = 'http://www.net-a-porter.com/product/192158';
	my $hostUrl = 'http://www.net-a-porter.com/';
	my $productID = 'ProductID';
 	my %product = $module->parseAndgetInfo($url,$hostUrl) if ( $EXEC{p} );
	print Dumper %product;
	

};
if ($@) {

  # E999999 : Crawler exit,because unknow error,and the error cause is recorded.
	Log::Write::setLog( 'E999999', { 'ERRID' => $@ } );
}
else {
	my $t1      = Benchmark->new;
	my $td      = timediff( $t1, $t0 );
	my $cputime = $td->[1] + $td->[2];

	# A999999 : Crawler process exits,and process id and CPU time is recorded.
	Log::Write::setLog( 'A999999', { 'CPU' => $cputime } );
}

# Delete process lock file.
unlink( $config->{'lockfile'} ) if ( -e $config->{'lockfile'} );

END {
	if ($?) {

  # E999999 : Crawler exit,because unknow error,and the error cause is recorded.
		Log::Write::setLog( 'E999999', { 'ERRID' => $? } );
	}
	else {

		# Send report mail to someone.
		# $module->sendMail() if($EXEC{e});
	}
}

1;
